package com.ljf.order.converter;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.ljf.order.dataobject.OrderDetail;
import com.ljf.order.dto.OrderDTO;
import com.ljf.order.enums.ResultEnum;
import com.ljf.order.exception.SellException;
import com.ljf.order.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.List;


/**
 * @Author: Ljf
 * @Date : 2019/2/2 17:07
 * @Description :
 */
@Slf4j
public class OrderForm2OrderDTOConverter {
    public static OrderDTO convert(OrderForm orderForm) {
        OrderDTO dto = new OrderDTO();
        dto.setBuyerName(orderForm.getName());
        dto.setBuyerAddress(orderForm.getAddress());
        dto.setBuyerOpenid(orderForm.getOpenid());
        dto.setBuyerPhone(orderForm.getPhone());
        List<OrderDetail> list;
        try {
            Gson gson = new Gson();
            list = gson.fromJson(orderForm.getItems(), new TypeToken<List<OrderDetail>>() {
            }.getType());
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
            log.error("【OrderFormDto 订单明细出错】,{}", orderForm.getItems());
            throw new SellException(ResultEnum.CART_ERROR);
        }
        dto.setOrderDetails(list);
        return dto;
    }
}
